package com.tsfot.spider.network;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsfot.spider.service.DataCollectionService;
import com.tsfot.spider.service.RollingFileService;
import com.tsfot.spider.task.SpiderTask;
import com.tsfot.spider.task.Task;

/**
 * 网络操作实现类
 * @author frank0
 *
 */
public class NetworkServiceImpl implements NetworkService {
	
	private final static Logger logger=LoggerFactory.getLogger(NetworkServiceImpl.class);
	
	private RollingFileService fileService;
	
	public Task download(String url,String name) {
		logger.info("下载【{}】资源...",url);
		/*try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		String content = HttpSender.getInstance().sendHttpGet(url);
		String localPath = fileService.writeStringToFile(name, content);
		SpiderTask task=new SpiderTask();
		task.setLocalPath(localPath);
		task.setFileName(name);
		return task;
	}

	public RollingFileService getFileService() {
		return fileService;
	}

	public void setFileService(RollingFileService fileService) {
		this.fileService = fileService;
	}

	public String downloadImage(String url,String fileName) {
		logger.info("下载【{}】图片...",url);
		/*try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		InputStream inputStream = HttpSender.getInstance().downloadImage(url);
		return fileService.writeImageToFile(inputStream,fileName);
	}
	
	
	
	
	
	
}
